//
//  OfflineStoreViewController.m
//  LiTianDecoration
//
//  Created by zero on 2018/7/13.
//  Copyright © 2018年 administrator. All rights reserved.
//
/**
 *  以6为基础屏幕的宽高的比例
 */
#define XW_WITH  [UIScreen mainScreen].bounds.size.width/375
#define XW_HIGHT [UIScreen mainScreen].bounds.size.height/667
#import "OfflineStoreViewController.h"
#import "MVGoodsDetailHomeViewController.h"
#import "XXOfflineRowCell.h"
#import "XXOfflineModel.h"
#import "XXRankCell.h"
@interface OfflineStoreViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**
 切换布局按钮
 */
@property (nonatomic,strong) UIButton *button_switch;
/**
 切换判断值
 */
@property (nonatomic,assign) BOOL isbool;
@property (nonatomic,strong) NSMutableArray *array_dataSource;
@property (weak, nonatomic) IBOutlet UICollectionView *coV;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@end

@implementation OfflineStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isbool = NO;
    [self makeItem];
    [self request:NO];
   
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.button_switch];
}

/**
 数据请求

 @param isbool <#isbool description#>
 */
- (void)request:(BOOL)isbool{
    NSDictionary *diction = @{@"clientType":@"ios"};
    __weak typeof(self)weakSelf = self;
    [[HTTPRequest shareApiHelper]GETExhibition:self.view UrlString:@"/" Parameters:diction Success:^(id resultObject) {
        NSArray *array = resultObject[@"itemList"];
        NSDictionary *dic = array[16];
        NSArray *goods = [dic[@"itemData"]mj_JSONObject];
        for (NSDictionary *dics in goods) {
           XXOfflineModel *model = [XXOfflineModel mj_objectWithKeyValues:dics];
           [weakSelf.array_dataSource addObject:model];
        }
        [weakSelf.coV reloadData];
    } Error:^{
        
    }];
}

/**
 cell 大小间距布局
 */
- (void)makeItem{
    _flowLayout.itemSize = _isbool == NO ? CGSizeMake((KScreenWidth-5)/2, 265*XW_WITH+15) :  CGSizeMake(KScreenWidth, 125);
    _flowLayout.minimumLineSpacing = _isbool == NO ? 5:0;
    _flowLayout.minimumInteritemSpacing = _isbool == NO? 5:0;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array_dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_isbool) {
        XXRankCell *cell = (XXRankCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"XXRankCell" forIndexPath:indexPath];
        cell.view_describe.hidden = YES;
        cell.model = self.array_dataSource[indexPath.row];
        return cell;
    } else {
        XXOfflineRowCell *cell = (XXOfflineRowCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"XXOfflineRowCell" forIndexPath:indexPath];
        cell.view_describe.hidden = YES; 
        cell.model = self.array_dataSource[indexPath.row];
        return cell;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    XXOfflineModel *model = self.array_dataSource[indexPath.row];
    MVGoodsDetailHomeViewController *GoodsDetailVC = [[MVGoodsDetailHomeViewController alloc] init];
    GoodsDetailVC.commond_id = model.commonId;
    [self.navigationController pushViewController:GoodsDetailVC animated:YES];
}

- (IBAction)buttonFootPrintAction:(id)sender {
}
- (IBAction)buttonGoTopAction:(id)sender {
}
- (void)buttonSwitchAction{
    
    self.button_switch.selected = !self.button_switch.selected;
    self.isbool = self.button_switch.selected;
    [self makeItem];
    [_coV reloadData];
}

- (UIButton *)button_switch{
    if (!_button_switch) {
        _button_switch = [UIButton buttonWithType:UIButtonTypeCustom];
        _button_switch.mj_size = CGSizeMake(30, 30);
        _button_switch.backgroundColor = [UIColor redColor];
        [_button_switch addTarget:self action:@selector(buttonSwitchAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button_switch;
}
- (NSMutableArray *)array_dataSource{
    if (!_array_dataSource) {
        _array_dataSource = [[NSMutableArray alloc]init];
    }
    return _array_dataSource;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
